package com.panopset.java;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * Provides a universal version number, separate from the individual projects
 * maven versions, which may, or may not change with a particular release.
 *
 * FormatSource changes the source code of this class so that the version
 * matches the date stamp of the day that FormatSource is run.
 *
 * @see com.panopset.java.FormatSource
 *
 * @author Karl Dinwiddie
 *
 */
public final class Version {

    /**
     * This long value is changed by FormatSource to match yyyyMMdd formatted
     * current date.
     */
    public static final long UNI_VERSION = 20110227L;

    /**
     * String representation of yyyyMMdd formatted current date, which is the
     * universal version of all Panopset software applications.
     */
    private String value;

    /**
     * Returns Panopset universal version number.
     *
     * @return yyyyMMdd formatted date as universal version number.
     */
    @Override
    public String toString() {
        if (value == null) {
            value = "" + UNI_VERSION;
        }
        return value;
    }

    /**
     * Recommendation would be to call this in a static initializer.
     * @param version New custom version.
     */
    public static void setVersion(final String version) {
        getInstance().value = version;
    }

    /**
     * yyyyMMdd.
     */
    public static final String SIMPLE_DATE_FORMAT = "yyyyMMdd";

    /**
     * yyyyMMdd.
     */
    public static final SimpleDateFormat VERSION_FORMAT = new SimpleDateFormat(
            SIMPLE_DATE_FORMAT);

    /**
     * @return Universal version, last creation date formatted as yyyyMMdd.
     */
    public static String getVersion() {
        return getInstance().toString();
    }

    /**
     * Called by FormatSource to update the run time version.
     *
     * @return New yyyyMMdd formatted date stamp.
     */
    String createNewVersion() {
        return VERSION_FORMAT.format(new Date());
    }

    /**
     * Based on SingletonHolder inner class by Bill Pugh.
     *
     * <h5>References</h5>
     * <ul>
     * <li>
     * <a href="http://en.wikipedia.org/wiki/Singleton_pattern">
     * http://en.wikipedia.org/wiki/Singleton_pattern </a></li>
     * </ul>
     *
     */
    private static final class Singleton {
        /**
         * Instance variable.
         */
        private static final Version INSTANCE = new Version();

        /**
         * Private constructor.
         */
        private Singleton() {
            // Prevent instantiation.
        }
    }

    /**
     * @return static FlagIcons instance.
     */
    protected static Version getInstance() {
        return Singleton.INSTANCE;
    }

    /**
     * Private singleton constructor.
     */
    private Version() { }
}
